package com.data4.supplier.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data4.feignapi.common.constants.GatewayConstants;
import com.data4.feignapi.dto.ProductDTO;
import com.data4.feignapi.dto.ProductPageDTO;
import com.data4.feignapi.dto.Result;
import com.data4.feignapi.entity.Product;
import com.data4.feignapi.vo.ProductDetailVO;
import com.data4.supplier.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Forever
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/product")
@Tag(name = "商品管理")
public class ProductController {
    @Resource
    private ProductService productService;

    @PostMapping("/addProduct")
    @Operation(summary = "添加商品")
    public Result addProduct(@RequestBody ProductDTO productDTO,
                             @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        return productService.addProduct(productDTO, supplierId);
    }

    //首页展示
    @GetMapping("/getProduct/{current}/{num}")
    @Operation(summary = "根据时间查询商品")
    public Result getProduct(@PathVariable Long current, @PathVariable Long num) {
        return productService.getProduct(current, num);
    }

    @GetMapping("/getProductByNameLike/Inter/{current}/{num}")
    @Operation(summary = "根据商品名称模糊查询商品,内部使用")
    public ProductPageDTO getProductByNameLikeInter(@RequestParam("productName") String productName,
                                                    @PathVariable Integer current,
                                                    @PathVariable Integer num) {
        return productService.getProductByNameLike(productName, current, num,null);
    }
    @GetMapping("/getProductByNameLike/{current}/{num}")
    @Operation(summary = "根据商品名称模糊查询商品")
    public Result getProductByNameLike(@RequestParam("productName") String productName,
                                       @PathVariable Integer current,
                                       @PathVariable Integer num) {
        return Result.ok(productService.getProductByNameLike(productName, current, num,null));
    }

    //这个方法是用户调用的
    @GetMapping("/getProductDetail/{productId}")
    @Operation(summary = "根据商品id查询商品详情")
    public ProductDetailVO getProductDetail(@PathVariable Integer productId) {
        return productService.getProductDetail(productId);
    }


    //这个方法使用户调用的
    @GetMapping("/getProductById/{productId}")
    @Operation(summary = "根据商品id查询商品")
    public Product getProductById(@PathVariable Integer productId) {
        return productService.lambdaQuery()
                .eq(Product::getProductId,productId).one();
    }

    @GetMapping("/getProductByMarkerPriceScope/{current}/{num}")
    @Operation(summary = "根据市场价格区间查询商品")
    public Result getProductByMarkerPriceScope(@PathVariable("current") Integer current,
                                               @PathVariable("num") Integer num,
                                               @RequestParam(required = false) Integer little,
                                               @RequestParam(required = false) Integer big) {
        return productService.getProductByMarkerPriceScope(current, num, little, big,null);
    }

    @PutMapping("/updateProduct/{productId}")
    @Operation(summary = "修改商品")
    public Result updateProduct(@RequestBody ProductDTO productDTO,
                                @PathVariable Integer productId) {
        return productService.updateProduct(productDTO, productId);
    }

    @DeleteMapping("/deleteProduct")
    @Operation(summary = "删除商品")
    public Result delete(@RequestBody List<Integer> productIds,
                         @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        return productService.deleteByIds(productIds, supplierId);
    }

}

